package com.elf.zero.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.TextSwitcher;
import android.widget.TextView;

import com.elf.zero.app.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class VerticalTextSwitcher extends TextSwitcher {

    private float textSize = 0;
    private int textColor = 0;
    private int duration = 500;
    private int interval = 3000;

    private Timer timer;
    private TimerTask timerTask;
    private Handler handler = new Handler(Looper.getMainLooper());

    private List<String> textItems = new ArrayList<>();
    private int index;

    private boolean pause = false;
    private final Object lock = new Object();


    public VerticalTextSwitcher(Context context) {
        this(context, null);
    }

    public VerticalTextSwitcher(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.VerticalTextSwitcher);
            textSize = typedArray.getDimension(R.styleable.VerticalTextSwitcher_textSize, textSize);
            textColor = typedArray.getColor(R.styleable.VerticalTextSwitcher_textColor, textColor);
            duration = typedArray.getInt(R.styleable.VerticalTextSwitcher_duration, duration);
            interval = typedArray.getInt(R.styleable.VerticalTextSwitcher_interval, interval);
            typedArray.recycle();
        }

        setFactory(new ViewFactory() {
            @Override
            public View makeView() {
                TextView textView = new TextView(getContext());
                if (textSize > 0) {
                    textView.setTextSize(textSize);
                }
                if (textColor != 0) {
                    textView.setTextColor(textColor);
                }
                return textView;
            }
        });

        TranslateAnimation inAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0);
        inAnimation.setDuration(duration);
        TranslateAnimation outAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -1);
        outAnimation.setDuration(duration);
        setInAnimation(inAnimation);
        setOutAnimation(outAnimation);
    }

    public void setTextList(List<String> list) {
        if (list == null || list.size() == 0) {
            return;
        }
        stop();
        textItems = list;
        index = 0;
        setCurrentText(textItems.get(index++));
//        setText(textItems.get(index++));

        if (textItems.size() > 1) {
            start();
        }
    }

    /**
     * 更新数据
     */
    public void updateTextList(List<String> list) {
        if (list == null || list.size() == 0) {
            return;
        }

        synchronized (lock) {
            textItems = list;
            index = 0;

            //暂停状态不处理
            if (pause) {
                return;
            }

            //没有暂停，且没有定时任务
            if (timerTask == null) {
                start();
            }
        }
    }

    private void start() {
        pause = false;
        timer = new Timer();
        timerTask = new TimerTask() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        synchronized (lock) {
                            if (index >= textItems.size()) {
                                index = 0;
                            }
                            if (index < textItems.size()) {
                                setText(textItems.get(index++));
                            }
                            if (textItems.size() == 1) {
                                stop();
                            }
                        }
                    }
                });
            }
        };
        timer.schedule(timerTask, interval, interval);
    }

    /**
     * 恢复
     */
    public void resume() {
        synchronized (lock) {
            if (textItems.size() > 1 && timerTask == null) {
                start();
            }
        }
    }

    /**
     * 暂停
     */
    public void pause() {
        stop();
        pause = true;
    }

    public void stop() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (timerTask != null) {
            timerTask = null;
        }
    }
}
